﻿namespace AC.ExtendedRenderer.Toolkit
{
    using ComponentFactory.Krypton.Toolkit;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Text;
    using System.Windows.Forms;

    [ToolboxBitmap(typeof(Panel)), DefaultProperty("HeaderText")]
    public class KryptonCollapseContainer : Panel
    {
        private Color _BorderColor = Color.FromArgb(0x65, 0x93, 0xcf);
        private bool _Collapsable = true;
        private Panel _ContentHider;
        private int _HeaderHeight = 20;
        private string _HeaderText;
        private bool _MouseOver;
        private bool _NoCheckControllAdded;
        private Color _NormalDarkColor = Color.FromArgb(0xd6, 0xe8, 0xff);
        private Color _NormalLightColor = Color.FromArgb(0xe3, 0xef, 0xff);
        private int _OpenHeight;
        private IPalette _palette;
        private PaletteRedirect _paletteRedirect;
        private bool _PanelOpen = true;
        private Color _RollOverDarkColor = Color.FromArgb(0xe3, 0xef, 0xff);
        private Color _RollOverLightColor = Color.White;
        private IContainer components;
        internal ImageList imgButtons;

        public KryptonCollapseContainer()
        {
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            base.UpdateStyles();
            base.FontChanged += new System.EventHandler(this.CollapseContainer_FontChanged);
            base.Paint += new PaintEventHandler(this.CollapseContainer_Paint);
            base.SizeChanged += new System.EventHandler(this.CollapseContainer_SizeChanged);
            base.MouseMove += new MouseEventHandler(this.CollapseContainer_MouseMove);
            base.MouseLeave += new System.EventHandler(this.CollapseContainer_MouseLeave);
            base.Click += new System.EventHandler(this.CollapseContainer_Click);
            this.BackColor = SystemColors.Control;
            this._HeaderText = base.Name.ToString();
            this._ContentHider = new Panel();
            this._ContentHider.Dock = DockStyle.Fill;
            this._ContentHider.Visible = false;
            this._NoCheckControllAdded = true;
            base.Controls.Add(this._ContentHider);
            this._NoCheckControllAdded = false;
            if (this._palette != null)
            {
                this._palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            KryptonManager.GlobalPaletteChanged += new System.EventHandler(this.OnGlobalPaletteChanged);
            this._palette = KryptonManager.CurrentGlobalPalette;
            this._paletteRedirect = new PaletteRedirect(this._palette);
            this._BorderColor = this._palette.ColorTable.ButtonSelectedBorder;
            this._NormalLightColor = this._palette.ColorTable.ToolStripGradientBegin;
            this._NormalDarkColor = this._palette.ColorTable.ToolStripGradientEnd;
            this._RollOverLightColor = Color.White;
            this._RollOverDarkColor = this._palette.ColorTable.ToolStripGradientEnd;
            this.InitializeComponent();
        }

        private void CollapseContainer_Click(object sender, EventArgs e)
        {
            if (this._Collapsable)
            {
                this._PanelOpen = !this._PanelOpen;
                this.SetContentPanel();
            }
        }

        private void CollapseContainer_FontChanged(object sender, EventArgs e)
        {
            this._HeaderHeight = ((int) ((Control) sender).Font.Size) + 11;
            if (!this._PanelOpen)
            {
                base.Height = this._HeaderHeight;
            }
            base.PerformLayout();
        }

        private void CollapseContainer_MouseLeave(object sender, EventArgs e)
        {
            if (this._Collapsable)
            {
                this._MouseOver = false;
                this.Refresh();
            }
        }

        private void CollapseContainer_MouseMove(object sender, MouseEventArgs e)
        {
            if (this._Collapsable)
            {
                if ((e.Y > 0) & (e.Y < 0x15))
                {
                    if (!this._MouseOver)
                    {
                        this._MouseOver = true;
                        this.Cursor = Cursors.Hand;
                        this.Refresh();
                    }
                }
                else if (this._MouseOver)
                {
                    this._MouseOver = false;
                    this.Cursor = Cursors.Default;
                    this.Refresh();
                }
            }
        }

        private void CollapseContainer_Paint(object sender, PaintEventArgs e)
        {
            if (this._palette == null)
            {
                EventArgs args = new EventArgs();
                this.OnGlobalPaletteChanged(this, args);
            }
            if (this._PanelOpen)
            {
                this._OpenHeight = base.Height;
            }
            e.Graphics.FillRectangle(new SolidBrush(this.BackColor), 0, 0, base.Bounds.Width, base.Bounds.Height);
            e.Graphics.FillRectangle(Brushes.White, 0, 0, base.Bounds.Width, base.Bounds.Height);
            Color separatorDark = this._palette.ColorTable.SeparatorDark;
            Color toolStripGradientBegin = this._palette.ColorTable.ToolStripGradientBegin;
            Color toolStripGradientMiddle = this._palette.ColorTable.ToolStripGradientMiddle;
            if (this._MouseOver)
            {
                toolStripGradientMiddle = toolStripGradientBegin;
                toolStripGradientBegin = Color.White;
            }
            Rectangle rect = new Rectangle(0, 0, base.Bounds.Width, this._HeaderHeight);
            LinearGradientBrush brush = new LinearGradientBrush(rect, Color.White, Color.White, LinearGradientMode.Horizontal);
            brush.WrapMode = WrapMode.TileFlipX;
            brush.SetSigmaBellShape(0.1f);
            ColorBlend blend = new ColorBlend(3);
            blend.Positions[0] = 0f;
            blend.Colors[0] = toolStripGradientMiddle;
            blend.Positions[1] = 0.5f;
            blend.Colors[1] = toolStripGradientBegin;
            blend.Positions[2] = 1f;
            blend.Colors[2] = toolStripGradientMiddle;
            brush.InterpolationColors = blend;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.CompositingMode = CompositingMode.SourceOver;
            e.Graphics.FillRectangle(brush, rect);
            if (this._Collapsable)
            {
                int y = (this._HeaderHeight / 2) - (this.imgButtons.Images[0].Height / 2);
                if (this._PanelOpen)
                {
                    e.Graphics.DrawImage(this.imgButtons.Images[0], base.Bounds.Width - 15, y);
                }
                else
                {
                    e.Graphics.DrawImage(this.imgButtons.Images[1], base.Bounds.Width - 15, y);
                }
            }
            e.Graphics.DrawLine(new Pen(separatorDark), 0, this._HeaderHeight - 1, base.Bounds.Width, this._HeaderHeight - 1);
            Font font = new Font(this.Font.FontFamily, this.Font.Size, this.Font.Style);
            StringFormat format = new StringFormat();
            SolidBrush brush2 = new SolidBrush(this._palette.ColorTable.StatusStripText);
            format.FormatFlags = StringFormatFlags.NoWrap;
            format.Trimming = StringTrimming.EllipsisCharacter;
            format.Alignment = StringAlignment.Near;
            format.LineAlignment = StringAlignment.Center;
            Rectangle layoutRectangle = new Rectangle(10, 2, base.Bounds.Width, this._HeaderHeight - 2);
            layoutRectangle.X = 10;
            layoutRectangle.Y = 2;
            layoutRectangle.Width = base.Bounds.Width;
            layoutRectangle.Height = this._HeaderHeight - 2;
            GraphicsPath path = new GraphicsPath();
            try
            {
                e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
                e.Graphics.DrawString(this._HeaderText, font, brush2, layoutRectangle, format);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
            path.Dispose();
            path = null;
            font.Dispose();
            font = null;
            format.Dispose();
            format = null;
            if (this.Dock != DockStyle.Fill)
            {
                e.Graphics.DrawLine(new Pen(this._palette.ColorTable.SeparatorDark), 0, base.Bounds.Height - 1, base.Bounds.Width, base.Bounds.Height - 1);
            }
        }

        [RefreshProperties(RefreshProperties.All)]
        private void CollapseContainer_SizeChanged(object sender, EventArgs e)
        {
            this.Refresh();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(KryptonCollapseContainer));
            this.imgButtons = new ImageList(this.components);
            base.SuspendLayout();
            this.imgButtons.ImageStream = (ImageListStreamer) manager.GetObject("imgButtons.ImageStream");
            this.imgButtons.TransparentColor = Color.Transparent;
            this.imgButtons.Images.SetKeyName(0, "UpArrow.gif");
            this.imgButtons.Images.SetKeyName(1, "DownArrow.gif");
            base.ResumeLayout(false);
        }

        private void OnGlobalPaletteChanged(object sender, EventArgs e)
        {
            if (this._palette != null)
            {
                this._palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            this._palette = KryptonManager.CurrentGlobalPalette;
            this._paletteRedirect.Target = this._palette;
            if (this._palette != null)
            {
                this._palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
                this._BorderColor = this._palette.ColorTable.ButtonSelectedBorder;
                this._NormalLightColor = this._palette.ColorTable.ToolStripGradientBegin;
                this._NormalDarkColor = this._palette.ColorTable.ToolStripGradientEnd;
                this._RollOverLightColor = Color.White;
                this._RollOverDarkColor = this._palette.ColorTable.ToolStripGradientEnd;
            }
            base.Invalidate();
        }

        private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            base.Invalidate();
        }

        private void SetContentPanel()
        {
            if (this._PanelOpen)
            {
                if (this.Dock == DockStyle.Fill)
                {
                    this._ContentHider.Visible = false;
                }
                else
                {
                    base.Height = this.OpenHeight;
                }
            }
            else if (this.Dock == DockStyle.Fill)
            {
                this._ContentHider.Visible = true;
                this._ContentHider.BringToFront();
            }
            else
            {
                this._OpenHeight = base.Height;
                base.Height = this._HeaderHeight;
            }
            this.Refresh();
        }

        [Browsable(false)]
        public override bool AllowDrop
        {
            get
            {
                return base.AllowDrop;
            }
            set
            {
                base.AllowDrop = value;
            }
        }

        [Browsable(false)]
        public override bool AutoScroll
        {
            get
            {
                return base.AutoScroll;
            }
            set
            {
                base.AutoScroll = value;
            }
        }

        [Browsable(false)]
        public override bool AutoSize
        {
            get
            {
                return base.AutoSize;
            }
            set
            {
                base.AutoSize = value;
            }
        }

        [Browsable(false)]
        public override Image BackgroundImage
        {
            get
            {
                return base.BackgroundImage;
            }
            set
            {
                base.BackgroundImage = value;
            }
        }

        [Browsable(false)]
        public override ImageLayout BackgroundImageLayout
        {
            get
            {
                return base.BackgroundImageLayout;
            }
            set
            {
                base.BackgroundImageLayout = value;
            }
        }

        public bool Collapsable
        {
            get
            {
                return this._Collapsable;
            }
            set
            {
                this._Collapsable = value;
                this.Refresh();
            }
        }

        public override Rectangle DisplayRectangle
        {
            get
            {
                Padding padding = base.Padding;
                return new Rectangle(padding.Left, this._HeaderHeight + padding.Top, base.Width - padding.Right, (base.Height - (this._HeaderHeight + 1)) - padding.Bottom);
            }
        }

        public string HeaderText
        {
            get
            {
                return this._HeaderText;
            }
            set
            {
                this._HeaderText = value;
                this.Refresh();
            }
        }

        public int OpenHeight
        {
            get
            {
                return this._OpenHeight;
            }
            set
            {
                this._OpenHeight = value;
            }
        }

        [RefreshProperties(RefreshProperties.All)]
        public bool PanelOpen
        {
            get
            {
                return this._PanelOpen;
            }
            set
            {
                this._PanelOpen = value;
                if (!this._PanelOpen)
                {
                    this.Collapsable = true;
                }
                this.SetContentPanel();
            }
        }
    }
}

